package org.item.user.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.item.common.bean.Customers;
import org.item.common.bean.SysUser;
import org.item.common.service.IUserService;
import org.item.common.util.ResultData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("user")
public class UserController {
    @Autowired
    IUserService userService;

    @PostMapping("/")
    public ResultData insert(@RequestBody SysUser user) {
        SysUser data = userService.insert(user);
        return ResultData.success(data);
    }

    @PutMapping("/")
    public ResultData update(@RequestBody SysUser user) {
        SysUser data = userService.update(user);
        return ResultData.success(data);
    }

    @DeleteMapping("/{id}")
    public ResultData delete(@PathVariable Integer id) {
        int result = userService.delete(id);
        if (result >= 1) {
            return ResultData.success(200, "删除成功", null);
        } else {
            return ResultData.fail();
        }
    }

    @GetMapping("/{id}")
    public ResultData getById(@PathVariable Integer id) {
        SysUser data = userService.getById(id);
        return ResultData.success(data);
    }

    @GetMapping("/list")
    public ResultData getList(SysUser user) {
        return ResultData.success(userService.getList(user));
    }

    @GetMapping("/page")
    public ResultData getListByPage(IPage page, @RequestBody SysUser user) {
        IPage data = userService.getListPage(page, user);
        return ResultData.success(data);
    }

    @PutMapping("/status/{id}")
    public ResultData setStatus(@PathVariable Integer id) {
        int data = userService.setStatus(id);
        return ResultData.success(data);
    }

    @GetMapping("/username/{username}")
    public ResultData getByUsername(@PathVariable String username) {
        SysUser data = userService.getByUsername(username);
        return ResultData.success(data);
    }
}